$(function () {
    
    var cmder = new Cmder;

    init();

    function init() {

        // last command input get focus, so you can input
        $(".command:last").focus();

        // listen the Enter keypress
        $(".command:last").bind("keydown", function (event) {
            if (event.keyCode == "13") {
                change($(".command:last").val());
            }
        });
    }

    function change(val) {

        var result = cmder.handleCmd(val);

        // reprint the Console Prompt
        var content = '<div>' + result + '</div>';

        // not finish yet
        if (!cmder.finish) { 

            content += '<div class="content">' + 
                       '<p>#root></p>' + 
                       '<input type="text" class="command">' + 
                       '</div>';
        }

        $(content).insertAfter($(".content:last"));

        if (cmder.finish) {
            $(".command:last").blur();
        } else {
            init();
        }
    }
});

